﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFIDA.U9.SM.Ship;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.PL;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.SM.RMA;
using UFIDA.U9.SM.SO;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.PR.PurchaseRequest;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.Base;
using UFSoft.UBF.Execution.WorkManagers;


namespace UFIDA.U9.Cust.Cli.PlugBE
{
    /// <summary>
    /// 中德项目
    /// 销售订单
    /// 保存时更新颜色
    /// </summary>
    class ZDSOExtend : IEventSubscriber
    {
        //配置XX.sub.xml文件放置到portal/bin下，dll文件放置到portal/ApplicationServer/Libs下
        public void Notify(params object[] args)
        {
            if (args != null && args.Length != 0 && args[0] is EntityEvent)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                SO be = key.GetEntity() as SO;
                if (be == null)
                    return;
                if (be.Status == SODocStatusEnum.Open || be.Status == SODocStatusEnum.Approving)
                {
                    //开立/提交 状态
                    //更新颜色
                    foreach (var line in be.SOLines)
                    {
                        if (line.ItemInfo?.ItemID == null)
                            continue;

                        if (line.ItemInfo.ItemID.ItemFormAttribute == ItemTypeAttributeEnum.Model)
                        {
                            //模型件
                            //没有生成FAS物料,不更新颜色
                            //if (line.FAS.ItemID == null) continue;

                            //大面
                            ItemMaster dm = ItemMaster.Finder.Find($"Code='{line.DescFlexField.PrivateDescSeg11}' and Org={Context.LoginOrg.ID}");
                            //小面
                            ItemMaster xm = ItemMaster.Finder.Find($"Code='{line.DescFlexField.PrivateDescSeg12}' and Org={Context.LoginOrg.ID}");
                            //通体
                            ItemMaster tt = ItemMaster.Finder.Find($"Code='{line.DescFlexField.PrivateDescSeg13}' and Org={Context.LoginOrg.ID}");
                            if (dm == null && xm == null && tt == null)
                            {
                                SetColor2Line(line);
                            }
                            else
                            {
                                //颜色=大面+通体+小面 名称
                                SetModalColor2Line(line, dm, xm, tt);
                            }

                        }
                        else
                        {
                            //非模型件
                            if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg11))
                                throw new Exception($"行号:[{line.DocLineNo}],不能录入大面");
                            if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg12))
                                throw new Exception($"行号:[{line.DocLineNo}],不能录入小面");
                            if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg13))
                                throw new Exception($"行号:[{line.DocLineNo}],不能录入通体");
                            if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg15))
                                throw new Exception($"行号:[{line.DocLineNo}],不能录入胶条");

                            if (line.ItemInfo.ItemID.ItemFormAttribute == ItemTypeAttributeEnum.MakePart)
                            {
                                //制造件
                                SetColor2Line(line);
                            }

                        }
                    }
                }
            }
        }
        /// <summary>
        /// 设置模型件颜色=大面/通体/小面
        /// </summary>
        /// <param name="line"></param>
        /// <param name="dm"></param>
        /// <param name="xm"></param>
        /// <param name="tt"></param>
        private static void SetModalColor2Line(SOLine line, ItemMaster dm, ItemMaster xm, ItemMaster tt)
        {
            List<string> strs = new List<string>();
            if (dm != null) strs.Add(dm.Name);
            if (tt != null) strs.Add(tt.Name);
            if (xm != null) strs.Add(xm.Name);
            line.DescFlexField.PrivateDescSeg16 = String.Join("/", strs);
        }

        /// <summary>
        /// 颜色=物料私有字段9 or 颜色=物料二级分类名称
        /// 二级分类编码=物料编码前六位
        /// </summary>
        /// <param name="line"></param>
        /// <exception cref="Exception"></exception>
        private static void SetColor2Line(SOLine line)
        {
            if (!string.IsNullOrEmpty(line.ItemInfo.ItemID.DescFlexField.PrivateDescSeg9))
            {
                //颜色=物料私有字段9
                line.DescFlexField.PrivateDescSeg16 = line.ItemInfo.ItemID.DescFlexField.PrivateDescSeg9;
            }
            else
            {
                //颜色 = 物料二级分类名称
                string path = $"Code='{line.ItemInfo.ItemCode.Substring(0, 6)}'";
                ItemCategory ic = ItemCategory.Finder.Find(path);
                if (ic == null) throw new Exception($"查询库存分类失败!path:{path}");
                line.DescFlexField.PrivateDescSeg16 = ic.Name;
            }
            
        }
    }
}
